package main

import (
	"fmt"
	"sync"
	"sync/atomic"
	"time"

)

var sum int32
func myFunc(i interface{}) {
	n := i.(int32)
	atomic.AddInt32(&sum, n)
	fmt.Printf("run with %d\n", n)
}

func demoFunc() {
	time.Sleep(time.Second)
	fmt.Println("Hello World!")
}

func main2()  {

	defer ants.Release() //关闭协程池
	runTimes := 1000000000000  //定义协程数量
	// Use the common pool.
	var wg sync.WaitGroup

	p, _ := ants.NewPool(20000)  //自定义池为5个
	//ants.ReSize(10)
	syncCalculateSum := func() {
		demoFunc()
		wg.Done()
	}

	for i := 0; i < runTimes; i++ {

		wg.Add(1)
		_ = p.Submit(syncCalculateSum)
	}
	wg.Wait()
	fmt.Printf("running goroutines: %d\n", ants.Running())
	fmt.Printf("finish all tasks.\n")

	// Use the pool with a function,
	// set 10 to the capacity of goroutine pool and 1 second for expired duration.
	//p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
	//	myFunc(i)
	//	wg.Done()
	//})
	//defer p.Release()
	//// Submit tasks one by one.
	//for i := 0; i < runTimes; i++ {
	//	wg.Add(1)
	//	_ = p.Invoke(int32(i))
	//}
	//wg.Wait()
	//fmt.Printf("running goroutines: %d\n", p.Running())
	//fmt.Printf("finish all tasks, result is %d\n", sum)

	//var t int = 2
	////t=1 //运行时间： 228.3839ms
	////运行时间： 15.6126ms
	//t1 := time.Now()
	//Enqueue(t)
	////time.Sleep(time.Second*2)
	//t2 := time.Now()
	//fmt.Println("运行时间：", t2.Sub(t1))
}



